/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.listener;

import com.minierp.dao.DbBackupDao;
import com.minierp.dao.EnterpriseInfoDao;
import com.minierp.dao.IdGenerator;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.DbBackup;
import com.minierp.model.EnterpriseInfo;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;

class DbBackupThread extends Thread {
	private boolean runFlag = true;
	private EnterpriseInfoDao enterpriseInfoDao;
	private DbBackupDao dbBackupDao;
	private IdGenerator idGenerator;
	private ServletContext context;

	public DbBackupThread(WebApplicationContext wac, ServletContext ct) {
		this.enterpriseInfoDao = (EnterpriseInfoDao) wac
				.getBean("enterpriseInfoDao");
		this.dbBackupDao = (DbBackupDao) wac.getBean("dbBackupDao");
		this.idGenerator = (IdGenerator) wac.getBean("idGenerator");
		this.context = ct;
	}

	public void setRunFlag(boolean runFlag) {
		this.runFlag = runFlag;
	}

	public void run() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String lastDbDel = "";

		while (this.runFlag) {
			try {
				Date e = new Date();
				Calendar c = Calendar.getInstance();
				EnterpriseInfo ent = this.enterpriseInfoDao.get();
				String fileName;
				if (ent.getDbAb().equals("Y")) {
					Date basePath = longSf.parse(sf.format(e) + " "
							+ ent.getDbAbHour() + ":" + ent.getDbAbMinute());
					if ((ent.getLastDbAb() == null || !ent.getLastDbAb()
							.equals(sf.format(e))) && basePath.before(e)) {
						String[] dfBackupFolder = PageHelper
								.getDbBackupFileName(this.context, ent);
						DbBackup dbList = new DbBackup();
						dbList.setId(this.idGenerator.getId(DbBackup.class
								.getName()));
						dbList.setCreaterId(0L);
						dbList.setCreaterName("系统自动");
						String i = "";
						if (dfBackupFolder[1] != null
								&& !dfBackupFolder[1].equals("")) {
							i = dfBackupFolder[0];
							dbList.setFileName(dfBackupFolder[1]);
						} else {
							dbList.setFileName(dfBackupFolder[0]);
						}

						this.dbBackupDao.save(dbList);
						String b = PageHelper.getMysqlBinPath(this.context);
						fileName = PageHelper.getDbPort(this.context);
						PageHelper.dbBackup(this.dbBackupDao, dbList.getId(),
								b, fileName, i);
						ent.setLastDbAb(sf.format(e));
						this.enterpriseInfoDao.update(ent);
					}
				}

				if (ent.getDbAdel().equals("Y")
						&& !lastDbDel.equals(sf.format(e))) {
					String arg32 = this.context.getRealPath("/");
					if (!arg32.endsWith(File.separator)) {
						arg32 = arg32 + File.separator;
					}

					String arg33 = arg32 + "WEB-INF" + File.separator
							+ "db_backup" + File.separator;
					c.setTime(e);
					c.add(5, -14);
					List arg34 = this.dbBackupDao.getList(c.getTime());

					for (int arg35 = 0; arg35 < arg34.size(); ++arg35) {
						DbBackup arg36 = (DbBackup) arg34.get(arg35);
						fileName = arg36.getFileName();
						int index = fileName.lastIndexOf(File.separator);
						String folder = fileName.substring(0, index);
						Runtime r = Runtime.getRuntime();
						Process p = null;
						String command = "cmd /C rd " + folder + " /s/q";

						try {
							p = r.exec(command);
							p.waitFor();
						} finally {
							if (p != null && p.getInputStream() != null) {
								p.getInputStream().close();
								p.getOutputStream().close();
								p.getErrorStream().close();
								p.destroy();
								p = null;
							}

						}

						this.dbBackupDao.delete(arg36.getId());
						index = folder.lastIndexOf(File.separator);
						if (index != -1) {
							String timePath = folder.substring(index + 1);
							if (timePath != null && !timePath.equals("")) {
								File file = new File(arg33 + timePath);
								if (file.exists() && file.isDirectory()) {
									command = "cmd /C rd " + arg33 + timePath
											+ " /s/q";

									try {
										p = r.exec(command);
										p.waitFor();
									} finally {
										if (p != null
												&& p.getInputStream() != null) {
											p.getInputStream().close();
											p.getOutputStream().close();
											p.getErrorStream().close();
											p.destroy();
											p = null;
										}

									}
								}
							}
						}
					}

					lastDbDel = sf.format(e);
				}
			} catch (Exception arg31) {
				arg31.printStackTrace();
			}

			try {
				EncCommonHelper
						.debug("************db backup:thread sleeping...");
				Thread.sleep(600000L);
			} catch (Exception arg28) {
				;
			}
		}

	}
}